package com.wnhz.work.jy.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.activity.AdminAccountActivity;
import com.wnhz.work.jy.activity.AssetRecordActivity;
import com.wnhz.work.jy.activity.AssetsActivity;
import com.wnhz.work.jy.activity.BankCardActivity;
import com.wnhz.work.jy.activity.CollectActivity;
import com.wnhz.work.jy.activity.CouponActivity;
import com.wnhz.work.jy.activity.CustomService;
import com.wnhz.work.jy.activity.GenerateOrderActivity;
import com.wnhz.work.jy.activity.LoginActivity;
import com.wnhz.work.jy.activity.MyAddressActivity;
import com.wnhz.work.jy.activity.MyOrderActivity;
import com.wnhz.work.jy.activity.PerformanceActivity;
import com.wnhz.work.jy.activity.QRCode;
import com.wnhz.work.jy.activity.ScoreConversionActivity;
import com.wnhz.work.jy.activity.SurplusActivity;
import com.wnhz.work.jy.activity.TakeGlodRecordActivity;
import com.wnhz.work.jy.activity.WebActivity;
import com.wnhz.work.jy.adapter.MyAddGridViewAdapter;
import com.wnhz.work.jy.bean.HomeFragment5Result;
import com.wnhz.work.jy.bean.MyAddGridViewBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.DisplayUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;
import com.wnhz.work.jy.view.MyScrollView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * ---我的
 */
public class HomeFragment5 extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener, MyScrollView.OnScrollListener {
    private static final String TAG = "HomeFragment5";
    private View view;
    private GridView gridView;//addView
    private LinearLayout linear;
    private int screenWidth;
    private List<MyAddGridViewBean> Mylist = new ArrayList<>();
    private MyAddGridViewAdapter mAdapter;
    private View my_line;
    private ImageView iv_head;
    private String headPath;
    private HomeFragment5Result fragment5Result;


    private ImageView head_img;
    private int[] pics = {R.mipmap.ic_wodejifenduihuan, R.mipmap.ic_woshoucang, R.mipmap.ic_wodedizhi,
            R.mipmap.ic_wodeyinghangka, R.mipmap.ic_wodelianxikefu,
            R.mipmap.ic_erweima, R.mipmap.ic_yejiguanlishengchengdingdan,R.mipmap.ic_yewuguanli};
    private String[] names = {"金豆兑换", "我的收藏", "我的地址", "绑定银行卡",
             "联系客服", "推荐码","生成订单", "业绩排行"};

    private MyMain myMain;

    private TextView tv_my_admin;//用户名
    private TextView tv_my_asset;//资产
    private TextView tv_my_surplus;//余额
    private TextView tv_my_bean;//金豆
    private TextView prompt_tv1, prompt_tv2, prompt_tv3, prompt_tv4, prompt_tv5, prompt_tv6, prompt_tv7, prompt_tv8;

    private List<TextView> prompt_tvs = new ArrayList<>();
    private List<String> prompt_strings = new ArrayList<>();
    private MyScrollView scrollView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.homefragment5, container, false);

        myMain = (MyMain) getActivity();
        initData();
        initView();

        scrollView.setOnScrollListener(this);

        return view;
    }


    @Override
    public void onResume() {
        scrollView.smoothScrollTo(scaleX, scaleY);
        scrollView.scrollTo(scaleX, scaleY);
        super.onResume();
        getData();
        setHeadImage();
    }

    private void initData() {
        for (int i = 0; i < 8; i++) {
            MyAddGridViewBean bean = new MyAddGridViewBean();
            bean.setImage(pics[i] + "");
            bean.setTitle(names[i]);

            Mylist.add(bean);
        }

    }

    private void initView() {
        scrollView = (MyScrollView) view.findViewById(R.id.scrollView);
        tv_my_admin = (TextView) view.findViewById(R.id.tv_my_admin);
        tv_my_asset = (TextView) view.findViewById(R.id.tv_my_asset);
        tv_my_surplus = (TextView) view.findViewById(R.id.tv_my_surplus);
        tv_my_bean = (TextView) view.findViewById(R.id.tv_my_bean);
        iv_head = (ImageView) view.findViewById(R.id.iv_my_head);
//        view.findViewById(R.id.rl_setting).setOnClickListener(this);
//        view.findViewById(R.id.tv_topup).setOnClickListener(this);//充值
//        view.findViewById(R.id.tv_extract).setOnClickListener(this);//提现
        head_img = (ImageView) view.findViewById(R.id.iv_my_head);
        view.findViewById(R.id.tv_account).setOnClickListener(this);//账户管理
        view.findViewById(R.id.iv_right).setOnClickListener(this);//设置图片

        view.findViewById(R.id.rl_myOrder).setOnClickListener(this);//我的订单
        view.findViewById(R.id.rl_pay).setOnClickListener(this);//待付款
        view.findViewById(R.id.rl_GoodsReceipt).setOnClickListener(this);//待收货
        view.findViewById(R.id.rl_returns).setOnClickListener(this);//退货单
        view.findViewById(R.id.rl_allOrder).setOnClickListener(this);//全部订单
        view.findViewById(R.id.scan_code).setOnClickListener(this);//扫码
        view.findViewById(R.id.tv_scan_code).setOnClickListener(this);//扫码

        view.findViewById(R.id.rl_my_wallet).setOnClickListener(this);//我的钱包
        view.findViewById(R.id.rl_asset).setOnClickListener(this);//我的资产
        view.findViewById(R.id.rl_surplus).setOnClickListener(this);//余额
        view.findViewById(R.id.rl_beans).setOnClickListener(this);//金豆
        view.findViewById(R.id.rl_coupon).setOnClickListener(this);//优惠券

        view.findViewById(R.id.rl_myAsset).setOnClickListener(this);//我的资产
        view.findViewById(R.id.rl_asset_record).setOnClickListener(this);//资产记录
        view.findViewById(R.id.rl_save_cold).setOnClickListener(this);//存金库
        view.findViewById(R.id.rl_extract_record).setOnClickListener(this);//提金记录
        view.findViewById(R.id.rl_sell_gold_record).setOnClickListener(this);//卖金记录


        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;

        my_line = view.findViewById(R.id.my_line);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DisplayUtil.dp2px(getActivity(), 0.5f));
        params.setMargins(0, 0, screenWidth / 4, 0);

        my_line.setLayoutParams(params);

        gridView = (GridView) view.findViewById(R.id.ll_my_gridView);
        mAdapter = new MyAddGridViewAdapter(getActivity(), Mylist, screenWidth);
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(this);


        prompt_tv1 = (TextView) view.findViewById(R.id.prompt_tv1);
        prompt_tv2 = (TextView) view.findViewById(R.id.prompt_tv2);
        prompt_tv3 = (TextView) view.findViewById(R.id.prompt_tv3);
        prompt_tv4 = (TextView) view.findViewById(R.id.prompt_tv4);
        prompt_tv5 = (TextView) view.findViewById(R.id.prompt_tv5);
        prompt_tv6 = (TextView) view.findViewById(R.id.prompt_tv6);
        prompt_tv7 = (TextView) view.findViewById(R.id.prompt_tv7);
        prompt_tv8 = (TextView) view.findViewById(R.id.prompt_tv8);
        prompt_tvs.add(prompt_tv1);
        prompt_tvs.add(prompt_tv2);
        prompt_tvs.add(prompt_tv3);
        prompt_tvs.add(prompt_tv4);
        //prompt_tvs.add(prompt_tv5);
        prompt_tvs.add(prompt_tv6);
        prompt_tvs.add(prompt_tv7);
        prompt_tvs.add(prompt_tv8);
        for (int i = 0; i < prompt_tvs.size(); i++) {
            prompt_tvs.get(i).setVisibility(View.GONE);
        }

//        linear = (LinearLayout) view.findViewById(R.id.ll_my_addgridview);
//
//        addGridView(linear);
    }


    private int scaleY;
    private int scaleX;

    @Override
    public void onPause() {
        super.onPause();
    }


    public void setHeadImage() {
        if (MyApplication.getInstance().userBean != null) {
            x.image().bind(head_img, MyApplication.getInstance().userBean.getUserImg(), MyApplication.getInstance().headoptions);
            tv_my_admin.setText(MyApplication.getInstance().userBean.getUserName());
            //tv_my_asset.setText("¥" + MyApplication.getInstance().userBean.getAssets());

            //tv_my_surplus.setText("¥" + MyApplication.getInstance().userBean.getSum());
            if ("1".equals(MyApplication.getInstance().userBean.getMember())) {
                mAdapter.setMember(true);
            } else {
                mAdapter.setMember(false);
            }
            mAdapter.notifyDataSetChanged();
        } else {
            myMain.onClick(myMain.tab_lay.get(0));
        }

    }


    private void addGridView(LinearLayout linear) {
        View view1 = getActivity().getLayoutInflater().inflate(R.layout.addgridview_my, null);
        gridView = (GridView) view1.findViewById(R.id.my_gridView);

        mAdapter = new MyAddGridViewAdapter(getActivity(), Mylist, screenWidth);
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(this);

        linear.addView(view1);
    }

    @Override
    public void onClick(View v) {
        Intent intent;

        switch (v.getId()) {

           /* //设置
            case R.id.rl_setting:
//                Toast.makeText(getActivity(), "设置", Toast.LENGTH_SHORT).show();
                intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                break;*/

           /* //充值
            case R.id.tv_topup:
                Toast.makeText(getActivity(), "充值", Toast.LENGTH_SHORT).show();
                break;

            //提现
            case R.id.tv_extract:
                Toast.makeText(getActivity(), "提现", Toast.LENGTH_SHORT).show();
                break;*/

            //设置
            case R.id.tv_account:
                intent = new Intent(getActivity(), AdminAccountActivity.class);
                startActivity(intent);
                break;

            //设置
            case R.id.iv_right:
                intent = new Intent(getActivity(), AdminAccountActivity.class);
                startActivity(intent);
                break;


            //我的订单
            case R.id.rl_myOrder:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("index", 0);
                startActivity(intent);
                break;

            //待付款
            case R.id.rl_pay:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("index", 1);
                startActivity(intent);
                break;

            //全部
            case R.id.rl_GoodsReceipt:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("index", 2);
                startActivity(intent);
                break;

            //已发货
            case R.id.rl_returns:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("index", 3);
                startActivity(intent);
                break;

            //待评价
            case R.id.rl_allOrder:
                intent = new Intent(getActivity(), MyOrderActivity.class);
                intent.putExtra("index", 4);
                startActivity(intent);
                break;

            //我的资产
            case R.id.rl_myAsset:
                startActivity(new Intent(getActivity(), AssetRecordActivity.class));
                break;

            //资产记录
            case R.id.rl_asset_record:
                startActivity(new Intent(getActivity(), AssetRecordActivity.class));
                break;

            //存金库
            case R.id.rl_save_cold:
                startActivity(new Intent(getActivity(), AssetsActivity.class));
                break;

            //提金记录
            case R.id.rl_extract_record:
                startActivity(new Intent(getActivity(), TakeGlodRecordActivity.class).putExtra("type", 3));
                break;

            //卖金记录
            case R.id.rl_sell_gold_record:
                startActivity(new Intent(getActivity(), TakeGlodRecordActivity.class).putExtra("type", 2));
                break;

            //我的钱包
            case R.id.rl_my_wallet:
                break;

            //我的资产
           /* case R.id.rl_asset:
                startActivity(new Intent(getActivity(), AssetsActivity.class));
                break;*/

            //账户余额
            case R.id.rl_surplus:
                intent = new Intent(getActivity(), SurplusActivity.class);
                startActivity(intent);
                break;

            //金豆
            case R.id.rl_beans:
                if (MyApplication.getInstance().userBean != null) {
                    intent = new Intent(getActivity(), WebActivity.class);
                    intent.putExtra("url", ConfigUtil.sign + MyApplication.getInstance().userBean.getUserId());
                    intent.putExtra("title", "签到");
                    startActivity(intent);
                } else {
//                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;

            //优惠券
            case R.id.rl_coupon:
                intent = new Intent(getActivity(), CouponActivity.class);
                startActivity(intent);
                break;

            case R.id.scan_code://扫码
                myMain.scanCode();
                break;

            case R.id.tv_scan_code://扫码
                myMain.scanCode();
                break;


            default:
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent;
        switch (i) {
            //积分兑换
            case 0:
                intent = new Intent(getActivity(), ScoreConversionActivity.class);
                startActivity(intent);
                break;

            //收藏
            case 1:
                intent = new Intent(getActivity(), CollectActivity.class);
                startActivity(intent);
                break;

            //我的地址
            case 2:
                intent = new Intent(getActivity(), MyAddressActivity.class);
                startActivity(intent);
                break;

            //绑定银行卡
            case 3:
                startActivity(new Intent(getActivity(), BankCardActivity.class));
                break;

            //联系客服
            case 4:
                /*intent = new Intent(getActivity(), IncomeAndExpensesDetailActivity.class);
                startActivity(intent);*/
                startActivity(new Intent(getActivity(), CustomService.class));
                break;

            //推荐码
            case 5:
                startActivity(new Intent(getActivity(), QRCode.class));
                break;


            case 6:
                startActivity(new Intent(getActivity(), GenerateOrderActivity.class));

                break;

            //业绩管理
            case 7:
                startActivity(new Intent(getActivity(), PerformanceActivity.class));
                break;
        }
    }


    public void getData() {
        if (MyApplication.getInstance().userBean != null) {
            Map<String, Object> map = new HashMap<>();
            myMain.showDialog();
            map.put("userId", MyApplication.getInstance().userBean.getUserId());
            XUtil.Post(ConfigUtil.MYINDEX, map, new MyCallBack<String>() {
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    Log.e(TAG, "onSuccess: " + result);
                    try {
                        JSONObject json = new JSONObject(result);
                        String re = json.optString("result");
                        if ("1".equals(re)) {
                            fragment5Result = new HomeFragment5Result();
                            fragment5Result = myMain.gson.fromJson(result, HomeFragment5Result.class);
                            setPrompt();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFinished() {
                    super.onFinished();
                    myMain.closeDialog();
                }
            });
        }

    }

    private void setPrompt() {
        prompt_strings.clear();
        prompt_strings.add(fragment5Result.getNoPayNums());
        prompt_strings.add(fragment5Result.getWaiteDeliveryNums());
        prompt_strings.add(fragment5Result.getWaiteReceiveNums());
        prompt_strings.add(fragment5Result.getWaiteEvaluationNums());
        //prompt_strings.add(fragment5Result.getAssetsNo());
        prompt_strings.add(fragment5Result.getAllNum());
        prompt_strings.add(fragment5Result.getTakeNo());
        prompt_strings.add(fragment5Result.getSellNo());

        if (fragment5Result.getUserInfo() != null)
            tv_my_bean.setText(fragment5Result.getUserInfo().getJindou());
        for (int i = 0; i < prompt_strings.size(); i++) {
            if (prompt_strings.get(i) == null || "".equals(prompt_strings.get(i)) || "0".equals(prompt_strings.get(i))) {
                prompt_tvs.get(i).setVisibility(View.GONE);
            } else {
                prompt_tvs.get(i).setVisibility(View.VISIBLE);
                prompt_tvs.get(i).setText(prompt_strings.get(i));
                /*if (prompt_strings.get(i).length() == 1){
                    prompt_tvs.get(i).setTextSize(11);
                }else {
                    prompt_tvs.get(i).setTextSize(8);
                }*/
            }
        }

    }

    @Override
    public void onScroll(int scrollY) {
        scaleY = scrollY;
    }
}
